import { SET_CURRENT_CATEGORY, GET_SLIDERS, SET_LESSONS_LOADING, SET_LESSONS, REFRESH_LESSONS } from '../action-types';
import { getSliders, getLessons, } from '@/api/home';
import { StoreGetState, StoreDispatch } from "../index";
export default {
  setCurrentCategory(currentCategory: string) {
    return {
      type: SET_CURRENT_CATEGORY,
      payload: currentCategory,
    };
  },
  getSliders() {
    return {
      type: GET_SLIDERS,
      payload: getSliders()
    };
  },
  getLessons() {
    return (dispatch: StoreDispatch, getState: StoreGetState) => {
      (async function () {
        const { currentCategory, lessons: { hasMore, offset, limit, loading } } = getState().home;
        if (hasMore && !loading) {
          dispatch({ type: SET_LESSONS_LOADING, payload: true });
          const result = await getLessons(currentCategory, offset, limit);
          dispatch({ type: SET_LESSONS, payload: result.data });
        }
      })();
    };
  },
  refreshLessons() {
    return (dispatch: StoreDispatch, getState: StoreGetState) => {
      (async function () {
        const { currentCategory, lessons: { hasMore, limit, loading } } = getState().home;
        if (hasMore && !loading) {
          dispatch({ type: SET_LESSONS_LOADING, payload: true });
          const result = await getLessons(currentCategory, 0, limit);
          dispatch({ type: REFRESH_LESSONS, paylaod: result.data });
        }
      })();
    };
  },
  getLesson() {

  }
};
